All files / Bandstand/src/app/api/songs/[id]/pdf route.ts

0% Statements 0/43
0% Branches 0/1
0% Functions 0/1
0% Lines 0/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44                                                                                       
export const dynamic = 'force-dynamic'
export const runtime = 'nodejs'

import {prisma} from '@/lib/db'
import {requireSession} from '@/lib/guard'
import {renderChartsPdf} from '@/lib/pdf'
import {buildPdfItem, paperFrom, pdfCues, pdfResponse} from '@/lib/chart-pdf'
import {route} from '@/lib/route'

// GET /api/songs/:id/pdf?key=A&version=3&paper=A4&download=1
export const GET = route(
  async (req: Request, {params}: {params: {id: string}}) => {
    const {user, band} = await requireSession()
    const url = new URL(req.url)
    const song = await prisma.song.findFirst({
      where: {id: params.id, bandId: band.id},
    })
    if (!song) return new Response('Not Found', {status: 404})
    const n = Number(url.searchParams.get('version'))
    const version = await prisma.chartVersion.findFirst({
      where: {
        songId: song.id,
        ...(Number.isInteger(n) && n > 0 ? {number: n} : {}),
      },
      orderBy: {number: 'desc'},
      include: {author: {select: {name: true, displayName: true, email: true}}},
    })
    if (!version) return new Response('Not Found', {status: 404})
    const item = buildPdfItem(band, song, version, {
      key: url.searchParams.get('key'),
    })
    // ?cues=1: with the reader's own cues (they asked for them on the PDF)
    if (url.searchParams.get('cues') === '1')
      item.cues = (await pdfCues(user.id, [song.id])).get(song.id)
    const buf = await renderChartsPdf([item], {paper: paperFrom(url)})
    const suffix = n > 0 ? ` (v${version.number})` : ''
    return pdfResponse(
      buf,
      song.title + suffix,
      url.searchParams.has('download'),
    )
  },
)